//app.js
App({
  onLaunch: function () {
    this.login();
  },

  login() {
    // 登录
    qq.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.callWxFun('getqqOpenid', { js_code: res.code }).then(res => {
          //console.log(res.openid);
          this.globalData.openid = res.openid
        })
      }
    })
  },

  /**
   * 获取微信Atoken
   */
  getAtoken() {
    const { wxId, wxKey } = this.globalData;
    let pro = new Promise((resolve, reject) => {
      let url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${wxId}&secret=${wxKey}`;
      qq.request({
        url,
        success: res => {
          resolve(res.data.access_token)
        }
      })
    });

    return pro;
  },

  /**
   * 调用微信云函数通用方法
   */
  callWxFun(name, params) {
    const { env } = this.globalData;
    let pro = new Promise((resolve, reject) => {
      this.getAtoken().then(res => {
        let url = `https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=${res}&env=${env}&name=${name}`;
        qq.request({
          url,
          method: 'post',
          data: params,
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            //console.log(res);
            let data = JSON.parse(res.data.resp_data);
            resolve(data);
          }
        })
      })
    })

    return pro;

  },

  /**
   * 获取微信云数据库记录 
   */
  catDatabase(sql) {
    const { env } = this.globalData;
    let pro = new Promise((resolve, reject) => {
      this.getAtoken().then(res => {
        let url = `https://api.weixin.qq.com/tcb/databasequery?access_token=${res}`;
        qq.request({
          url,
          method: 'post',
          data: {
            env,
            query: sql
          },
          success(res) {
            let strArr = res.data.data;
            let arr = strArr.map(v => {
              return JSON.parse(v);
            })
            resolve(arr);
          }
        })
      })
    });
    return pro
  },

  /**
   * 统计微信云数据库数量
   */

  countDatabase(sql) {
    const { env } = this.globalData;
    let pro = new Promise((resolve, reject) => {
      this.getAtoken().then(res => {
        let url = `https://api.weixin.qq.com/tcb/databasecount?access_token=${res}`;
        qq.request({
          url,
          method: 'post',
          data: {
            env,
            query: sql
          },
          success(res) {
            resolve(res.data.count);
          }
        })
      })
    });
    return pro
  },

  /**
   * 获取云存储图片
   */
  loadWxImg(file_list) {
    const { env } = this.globalData;
    let pro = new Promise((resolve, reject) => {
      this.getAtoken().then(res => {
        let url = `https://api.weixin.qq.com/tcb/batchdownloadfile?access_token=${res}`;
        qq.request({
          url,
          method: 'post',
          data: {
            env,
            file_list
          },
          success(res) {
            let list=res.data.file_list;
            let images=list.map(v=>{
              return v.download_url
            })
            resolve(images);
          }
        })
      })
    });

    return pro;
  },

  globalData: {
    openid: null,
    userInfo: null,
    wxId: 'wxb9d97097c84546ca',
    wxKey: '3ea9a956797a16837e5cdb337d4a1033',
    env: 'online-00000'
  }
})
